package com.study.demo.aop.demo3;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 注解方式的 Cache
 * @author: elvin
 * @time: 2019-05-21 09:49
 * @desc:
 **/
public class Start3 {
    public static void main(String[] args){
        /*ApplicationContext ct  = new ClassPathXmlApplicationContext("classpath:demo1/aop.xml");
       IUserService service = (IUserService)ct.getBean("userService");
        System.out.println("==========第一次获取====================");
        User user = service.getDefault();
        System.out.println("方法调用结束, 获取到的结果为: " + user.toString());

        System.out.println("==========第二次获取====================");
        User user1 = service.getDefault();
        System.out.println("方法调用结束, 获取到的结果为: " + user1.toString());
*/

        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(Config.class);
        IUserService userService = ac.getBean(IUserService.class);
        cacheAblePutEvict(userService);
        //cacheAblePutEvict(service);
    }

    private static void sleep(){
        try{
            Thread.sleep(111);
        }
        catch(Exception e){

        }
    }

    /**
     * 验证 Cacheable 和 CachePut 的作用
     * @param service
     */
    private static void cacheAblePutEvict(IUserService service) {
       /* System.out.println("==========getById 1 第一次获取====================");
        User user = service.getById(1);
        System.out.println(user.toString());

        sleep();

        System.out.println();
        System.out.println("========== getById 1 第二次获取====================");
        user = service.getById(1);
        System.out.println(user.toString());

        sleep();

        System.out.println();
        System.out.println("========== getById 2 第一次获取====================");
        user = service.getById(2);
        System.out.println(user.toString());

        sleep();

        System.out.println();
        System.out.println("==========getByIdPut 3 第一次获取====================");
        user = service.getByIdPut(3);
        System.out.println(user.toString());

        sleep();*/

        System.out.println();
        System.out.println("==========getById 3 第一次获取====================");
        User user = service.getById(3);
        System.out.println(user.toString());

        sleep();

        System.out.println();
        System.out.println("==========getByIdPut 3 第二次获取====================");
        user = service.getByIdPut(3);
        System.out.println(user.toString());

        /*sleep();

        System.out.println();
        System.out.println("==========getById 3 第二次获取====================");
        user = service.getById(3);
        System.out.println(user.toString());

        sleep();

        System.out.println();
        System.out.println("==========clearBy 3 第一次清除====================");
        service.clearBy(3);

        sleep();

        System.out.println();
        System.out.println("==========getById 3 第三次获取====================");
        user = service.getById(3);
        System.out.println(user.toString());*/
    }
}
